package com.toocms.jinjuhang.dialog.hint

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.TextView
import androidx.fragment.app.DialogFragment
import com.blankj.utilcode.util.StringUtils
import com.toocms.jinjuhang.R

class HintDialog : DialogFragment() {
    private var titleTv: TextView? = null
    private var contentTv: TextView? = null
    private var passiveTv: TextView? = null
    private var positiveTv: TextView? = null
    var onPassiveClickListener: ((tag: String?) -> Unit)? = null
    var onPositiveClickListener: ((tag: String?) -> Unit)? = null

    var title: CharSequence = StringUtils.getString(R.string.str_hint)
        set(value) {
            field = value
            titleTv?.text = value
            titleTv?.visibility = if (TextUtils.isEmpty(value)) View.GONE else View.VISIBLE
        }
    var content: CharSequence = ""
        set(value) {
            field = value
            contentTv?.text = value
        }
    var passive: CharSequence = StringUtils.getString(R.string.str_cancel)
        set(value) {
            field = if (TextUtils.isEmpty(value))
                StringUtils.getString(R.string.str_cancel)
            else
                value
            passiveTv?.text = field
        }
    var positive: CharSequence = StringUtils.getString(R.string.str_confirm)
        set(value) {
            field = if (TextUtils.isEmpty(value))
                StringUtils.getString(R.string.str_confirm)
            else
                value
            positiveTv?.text = field
        }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.dialog_hint, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        view.apply {
            titleTv = findViewById(R.id.title_tv)
            contentTv = findViewById(R.id.content_tv)
            passiveTv = findViewById(R.id.passive_tv)
            positiveTv = findViewById(R.id.positive_tv)
        }
        titleTv?.text = title
        titleTv?.visibility = if (TextUtils.isEmpty(title)) View.GONE else View.VISIBLE
        contentTv?.text = content
        passiveTv?.text = passive
        positiveTv?.text = positive
        passiveTv?.setOnClickListener {
            dismiss()
            onPassiveClickListener?.invoke(tag)
        }
        positiveTv?.setOnClickListener {
            dismiss()
            onPositiveClickListener?.invoke(tag)
        }
    }

    override fun onStart() {
        super.onStart()
        config()
    }

    private fun config() {
        dialog?.run {
            setCancelable(true)
            setCanceledOnTouchOutside(true)
            window
        }?.apply {
            setLayout(
                (displayMetrics().widthPixels * 0.75f).toInt(),
                WindowManager.LayoutParams.WRAP_CONTENT
            )
            setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        }
    }


    private fun displayMetrics() = resources.displayMetrics
}